import {
  getNoticeThemeList,
  getMsgThemeType,
  getNoticeThemeType,
  addNotice,
  editNotice,
  startStatus,
} from "../server/noticeApi";

const initData = {
  noticeThemeList: [],
  msgThemeTypeList: [],
  noticeThemeTypeList: [],
  totalCount: 0,
};

export const NOTICE_MSG_MODEL_NAMESPACE = 'notice_msg';
export const prefixedAction = name => `${NOTICE_MSG_MODEL_NAMESPACE}/${name}`;
export default {
  namespace: NOTICE_MSG_MODEL_NAMESPACE,
  state: {
    ...initData,
  },

  effects: {
    *getNoticeThemeList({ payload }, { call, put }) {
      const { errCode, errMsg, errortext, datas, totalCount } = yield call(
        getNoticeThemeList,
        payload,
      );
      yield put({ type: "get/getNoticeThemeList", payload: datas, totalCount });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *getNoticeTheme({ payload }, { call }) {
      const { type, noticeThemCode } = payload;
      const { errCode, errMsg, errortext, datas } = yield call(
        getNoticeThemeList,
        { pageSize: 1, pageNum: 1, type, status: 1, noticeThemCode } ,
      );
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *getMsgThemeType(_, { call, put, select }) {
      const { msgThemeTypeList } = yield select(({ [NOTICE_MSG_MODEL_NAMESPACE]:m }) => m);
      if (msgThemeTypeList.length > 0) {
        return {
          errCode: 0,
          datas: msgThemeTypeList,
        }
      }
      const { errCode, errMsg, errortext, datas } = yield call(getMsgThemeType);
      yield put({ type: "get/getMsgThemeType", payload: datas });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *getNoticeThemeType(_, { call, put, select }) {
      const { noticeThemeTypeList } = yield select(({ [NOTICE_MSG_MODEL_NAMESPACE]:m }) => m);
      if (noticeThemeTypeList.length > 0) {
        return {
          errCode: 0,
          datas: noticeThemeTypeList,
        }
      }
      const { errCode, errMsg, errortext, datas } = yield call(getNoticeThemeType);
      yield put({ type: "get/getNoticeThemeType", payload: datas });
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *addNotice({ payload }, { call }) {
      const resp = yield call(addNotice, payload);
      return resp;
    },
    *editNotice({ payload }, { call }) {
      const resp = yield call(editNotice, payload);
      return resp;
    },
    *deleteNotice({ payload }, { call }) {
      const resp = yield call(editNotice, { ...payload, status: 2 });
      return resp;
    },
    *startStatus({ payload }, { call }) {
      const resp = yield call(startStatus, payload);
      return resp;
    },
  },
  reducers: {
    "get/getNoticeThemeList": (state, { payload, totalCount }) => ({
      ...state,
      noticeThemeList: payload,
      totalCount,
    }),
    "get/getMsgThemeType": (state, { payload }) => ({
      ...state,
      msgThemeTypeList: payload,
    }),
    "get/getNoticeThemeType": (state, { payload }) => ({
      ...state,
      noticeThemeTypeList: payload,
    }),
    clearData: () => ({ ...initData }),
  },
};
